<?php
include  'loginCheck.php';
include  'Util.php';

if (isset($_POST['noTextId'])) {
    header("location: ".Constants::$link_editDownloads.$_POST['element_1']);
}

if ($_POST['edit']) {
    $link = Util::getLink();

  /* if ($_FILES["element_3"]["error"] == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["element_3"]["tmp_name"];
        $name = $_FILES["element_3"]["name"];
        $file = "/download_".date('Ymdhis')."_".$name;
        $fullUploadDir = Constants::$uploads_dir.$file;
        move_uploaded_file($tmp_name, $fullUploadDir);
        $dbPath = Constants::$uploads_dir_fromWeb.$file;
    }else{
        $dbPath = $_POST['oldFile'];
    }*/
    // zapis textu
    $query = sprintf(Util::$insert_jornakText,
        mysql_real_escape_string($_POST['element_1']),
        mysql_real_escape_string($_POST['element_3']),
        mysql_real_escape_string($_POST['element_2']), //section
        mysql_real_escape_string(null),
        mysql_real_escape_string(null),
        mysql_real_escape_string(null)
    );


    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }    

    // Zapis do destination
    $query = sprintf(Util::$insert_jornakDestination,
        mysql_real_escape_string($_POST['form_type']),
        mysql_real_escape_string(mysql_insert_id()),
        //2011-04-11 16:01:31
        mysql_real_escape_string(Util::getCurrentDateTime())
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }


    $query = sprintf(Util::$delete_jornakDestinationByTextId,
        mysql_real_escape_string($_POST['oldTextId'])
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }

    header("location: ".Constants::$link_done_fromResolve);
}

if (isset($_POST['delete'])) {
    $link = Util::getLink();
    for ($i = 1; $i < $_POST['count']+1; $i++) {
        if (isset($_POST['element_1_'.$i])) {
            $query = sprintf(Util::$delete_jornakDestination,
                mysql_real_escape_string($_POST['element_1_'.$i])
            );
            $result = mysql_query($query);

            // Check result
            if (!$result) {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
        //echo ("Query succesful. <br>");
        }
    }
    Util::closeLink($link);

    header("location: ".Constants::$link_done_fromResolve);
}
if (isset($_POST['new'])) {
    $link = Util::getLink();

    /*if ($_FILES["element_8"]["error"] == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["element_8"]["tmp_name"];
        $name = $_FILES["element_8"]["name"];
        $file = "/download_".date('Ymdhis')."_".$name;
        $fullUploadDir = Constants::$uploads_dir.$file;
        move_uploaded_file($tmp_name, $fullUploadDir);
    }*/
    // zapis textu
    $query = sprintf(Util::$insert_jornakText,
        mysql_real_escape_string($_POST['element_1']),
        mysql_real_escape_string($_POST['element_8']),
        mysql_real_escape_string($_POST['element_9']), //section
        mysql_real_escape_string(null),
        mysql_real_escape_string(null),
        mysql_real_escape_string(null)
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
    //echo ("First query succesful. <br>");

    // Zapis do destination
    $query = sprintf(Util::$insert_jornakDestination,
        mysql_real_escape_string($_POST['form_type']),
        mysql_real_escape_string(mysql_insert_id()),
        //2011-04-11 16:01:31
        mysql_real_escape_string(Util::getCurrentDateTime())
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
    Util::closeLink($link);

    header("location: ".Constants::$link_done_fromResolve);
//echo ("Second query succesful. <br>");
// Free the resources associated with the result set
// This is done automatically at the end of the script
//mysql_free_result($result);
}



?>

